package org.maiyoframework.assist.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.maiyoframework.assist.entity.User;
import org.maiyoframework.assist.service.UserResSvc;
import org.maiyoframework.assist.service.UserSvc;
import org.maiyoframework.core.common.Captcha;
import org.maiyoframework.core.web.fileupload.CommonsUploadFactory;
import org.maiyoframework.core.web.fileupload.SpringUploadFactory;
import org.maiyoframework.core.web.fileupload.UploadFile;
import org.maiyoframework.core.web.page.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestAct {

	@RequestMapping("/register")
	public String register() {
		return "WEB-INF/content/register.jsp";
	}
	
	@RequestMapping("/submit")
	public String submit(User user, String name, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		model.addAttribute("name", name);
		model.addAttribute("model",user);
		return "WEB-INF/content/view.jsp";
	}
	
	@RequestMapping("/edit/{userId}")
	public void edit(@PathVariable("userId") String id,
			HttpServletResponse response) {
		try {
			response.getWriter().write("USERID:" + id);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/updater_view")
	public String updater_view(ModelMap model) {
		User user = userSvc.findById(68);
		model.addAttribute("user", user);
		return "WEB-INF/content/updater.jsp";
	}
	
	@RequestMapping("/updater")
	public void updater(User user, HttpServletRequest request,
			HttpServletResponse response) {
		userSvc.updateDynamic(user);
	}
	
	@RequestMapping("/redirectUrl")
	public String redirect(){
		return "redirect:/register";
	}
	
	@RequestMapping("/list")
	public String list(Integer p, HttpServletRequest request, ModelMap model){
		Page<User> page = userSvc.findPage(request, p, 3);
		model.addAttribute("list", page);
		return "WEB-INF/content/list.jsp";
	}
	
	@RequestMapping("/captcha")
	public void captcha(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Captcha.getLite(request, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// commons-fileupload 上传多个
	@RequestMapping("/upMany")
	public String upMany() {
		return "WEB-INF/content/upload_many.jsp";
	}
	
	// commons-fileupload 上传单个
	@RequestMapping("/upOne")
	public String upOne() {
		return "WEB-INF/content/upload_one.jsp";
	}
	
	// springmvc 上传
	@RequestMapping("/mvcup")
	public String mvcup() {
		return "WEB-INF/content/mvc_upload.jsp";
	}
	
	@RequestMapping("/uploadMany")
	public void uploadMany(HttpServletRequest request, HttpServletResponse response) {
		CommonsUploadFactory factory = new CommonsUploadFactory("res/upload/");
//		FileUploadFactory factory = new FileUploadFactory("res/upload/", (long) (1024 * 1024 * 10), ".exe.zip");
//		factory.setSizeThreshold(1000 * 1000 * 2);
//		factory.setSizeMax((long) (1024*1024*10));
		if (factory.upload(request)) {
			List<FileItem> items = factory.getFileItems();
			for(FileItem it : items){
				if(it.isFormField()){
					if("file".equals(it.getFieldName())){
						System.out.println(it.getString());
					}
				}
			}
			System.out.println("factory>>>>>>>>>>>>>>>>>>>>>");
			System.out.println("允许后缀"+factory.getAllowExt());
			System.out.println("上传路径"+factory.getPath());
			System.out.println("上传目录"+factory.getSavePath());
			System.out.println("单个文件大小"+factory.getFileSizeMax());
			System.out.println("总大小"+factory.getSizeMax());
			System.out.println("内存"+factory.getSizeThreshold());
			System.out.println("表单数"+factory.getFileItems().size());
			List<UploadFile> ufs = factory.getUploadfiles();
			for(UploadFile u : ufs){
				if(u.getIsDone()){
					System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
					System.out.println("原名称"+u.getOriginalName());
					System.out.println("新名称"+u.getName());
					System.out.println("文件地址"+u.getUrlPath());
					System.out.println("文件大小"+u.getSize());
					System.out.println("后缀"+u.getFileExt());
					System.out.println("错误信息"+u.getErrorMessage());
				}else{
					System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
					System.out.println("原名称"+u.getOriginalName());
					System.out.println("新名称"+u.getName());
					System.out.println("文件地址"+u.getUrlPath());
					System.out.println("文件大小"+u.getSize());
					System.out.println("后缀"+u.getFileExt());
					System.out.println("错误信息"+u.getErrorMessage());
				}
			}
		} else {
			System.out.println(factory.getErrorMessage());
		}
	}
	
	@RequestMapping("/uploadOne")
	public void uploadOne(HttpServletRequest request, HttpServletResponse response) {
		CommonsUploadFactory factory = new CommonsUploadFactory("res/upload/");
		if (factory.upload(request)) {
			UploadFile uf = factory.getSingleUploadFile();
			if (uf.getIsDone())
				System.out.println(uf.getOriginalName() + " 已上传至 " + uf.getUrlPath());
			else
				System.out.println(uf.getErrorMessage());
		} else {
			System.out.println(factory.getErrorMessage());
		}
	}
	
	@RequestMapping("/mvcUpload")
	public void mvcUpload(String name, HttpServletRequest request, HttpServletResponse response){
		System.out.println("name:" + name);
		SpringUploadFactory factory = new SpringUploadFactory("res/upload");
//		factory.setAllowExt(".doc");
//		factory.setMaxUploadSize((long) (1024*1024*15));
//		factory.setMaxInMemorySize(1024*1024*10);
//		factory.setFileSizeMax((long) (1024*1024));
		if (factory.upload(request, "file")) {
			System.out.println("factory>>>>>>>>>>>>>>>>>>>>>");
			System.out.println("允许后缀"+factory.getAllowExt());
			System.out.println("上传路径"+factory.getPath());
			System.out.println("上传目录"+factory.getSavePath());
			System.out.println("单个文件大小"+factory.getFileSizeMax());
			List<UploadFile> ufs = factory.getUploadfiles();
			for(UploadFile u : ufs){
				if(u.getIsDone()){
					System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
					System.out.println("原名称"+u.getOriginalName());
					System.out.println("新名称"+u.getName());
					System.out.println("文件地址"+u.getUrlPath());
					System.out.println("文件大小"+u.getSize());
					System.out.println("后缀"+u.getFileExt());
					System.out.println("错误信息"+u.getErrorMessage());
				}else{
					System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
					System.out.println("原名称"+u.getOriginalName());
					System.out.println("新名称"+u.getName());
					System.out.println("文件地址"+u.getUrlPath());
					System.out.println("文件大小"+u.getSize());
					System.out.println("后缀"+u.getFileExt());
					System.out.println("错误信息"+u.getErrorMessage());
				}
			}
		} else {
			System.out.println(factory.getErrorMessage());
		}
	}
	
	@Resource
	private UserSvc userSvc;
	@Resource
	private UserResSvc userResSvc;

}
